package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.game.character.AbstractCharacter;
import se.webbzon.boltzmann.game.character.CharacterActionEnum;
import se.webbzon.boltzmann.minigame.chemistry.AmmoniaMolecule;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.HydrogenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitrogenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SugarMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.effect.SandBoxSun;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;

public class PhotosynthesisCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double x0 = 0, y0 = 0; //r.getMinX(), y0 = r.getMinY();
		double w = r.getWidth(), h = r.getHeight();
		
		DefaultSandBoxContainer co2, o2, h2o, c6h12o6;
		co2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		co2.setPosition(x0 + 32, y0 + 96);
		o2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		o2.setPosition(x0 + 400, y0 + 64);
		h2o = new DefaultSandBoxContainer(SandBoxMolecule.class);
		h2o.setPosition(x0 + 32, y0 + 224);
		c6h12o6 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		c6h12o6.setPosition(x0 + 400, y0 + 128);
		
		SandBoxPicture plant, slug;
		plant = new SandBoxPicture(Resources.getImageSet("biology_plant"),50);
		plant.setPosition(x0 + 192, y0 + 104);
		plant.setSize(128,128);
		plant.setSpeed(0.01);
		ResourceSet<Texture3D> textureSet = AbstractCharacter.getCharacterTextureSetMap().get("slug02").getLeftDown(CharacterActionEnum.WALK);
		slug = new SandBoxPicture(textureSet,50);
		slug.setPosition(x0 + 568, y0 + 136);
		slug.setSize(64, 64);
		slug.setSpeed(0.2);
		
		Earth earth = new Earth();
		earth.setSize(earth.getWidth(), 96);
		SandBoxSun sun = new SandBoxSun();
		sun.setHradius(320);
		sun.setVradius(192);
		sun.setDeltaTime(Math.PI/600);
		sun.setPosition(r.getWidth()/2, r.getHeight());
		
		/** Add anchors and arrows**/
		SandBoxAnchor an1 = new DefaultSandBoxAnchor(x0 + 32,y0 + 180);
		SandBoxAnchor an2 = new DefaultSandBoxAnchor(x0 + 176,y0 + 180);
		SandBoxAnchor an3 = new DefaultSandBoxAnchor(x0 + 568,y0 + 224);
		SandBoxAnchor an4 = new DefaultSandBoxAnchor(x0 + 568,y0 + 24);
		SandBoxAnchor an5 = new DefaultSandBoxAnchor(x0 + 32,y0 + 24);
		
		SandBoxArrow[] arrows = {
				new SandBoxArrow(plant,o2),
				new SandBoxArrow(o2,slug),
				new SandBoxArrow(plant,c6h12o6),
				new SandBoxArrow(c6h12o6,slug),
				new SandBoxArrow(h2o,an2),
				new SandBoxArrow(co2,an2),
				new SandBoxArrow(slug,h2o),
				new SandBoxArrow(slug,co2)
		};
		arrows[4].addAnchor(an1);
		arrows[5].addAnchor(an1);
		arrows[6].addAnchor(an3);
		arrows[7].addAnchor(an4);
		arrows[7].addAnchor(an5);
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				co2, o2, h2o, c6h12o6,
				plant, slug, earth, sun,
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new CarbonDioxideMolecule(),
				new OxygenMolecule(), new SugarMolecule(),
				new NitrogenMolecule(), new AmmoniaMolecule(),
				new HydrogenMolecule()
		};
		
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	x0 + (0.1 + 0.8 * w * Math.random()), 
							y0 + (0.1 + 0.8 * h * Math.random()));
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(co2,CarbonDioxideMolecule.class);
		addSolution(o2,OxygenMolecule.class);
		addSolution(h2o,WaterMolecule.class);
		addSolution(c6h12o6,SugarMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}
	
	@Override public Difficulty getDifficulty() {
		return Difficulty.MEDIUM;
	}

}
